<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script src="./js/earthsdk.js"></script>
    <!-- 自定义Css样式 -->
    <link rel="stylesheet" href="./css/example1.css">
    <!-- Vue文件 -->
    <script src="./js/vue.global.js"></script>
    <title>根据json加载视角管理器</title>
    <style>
        html,
        body,
        #app {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }

        .content {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-start;
            align-content: flex-start;
            min-height: 130px;
            max-height: 700px;
            overflow-y: auto;
        }

        .content>div {
            width: 64px;
            height: 89px;
            display: flex;
            flex-direction: column;
            position: relative;
            margin-right: 8px;
            margin-bottom: 10px;
        }

        .content>div input {
            text-indent: 0px;
            background: none;
            border: none;
            color: #fff;
            text-align: center;
        }

        .content>div p {
            padding: 0px;
            text-align: center;
            margin: 0px;
        }

        .close {
            position: absolute;
            cursor: pointer;
            right: 0px;
            top: 0px;
        }

        .focus {
            border: 1px solid #00FFFF;
        }

        #panel>a {
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div id="app"></div>
    <div id="panel">
        <view-collection :viewCollection="viewCollection"></view-collection>
        <a href="https://www.earthsdk.com/earthui/index.html" target="_blank">配置json链接 </a>
    </div>
    <script>
        /** ---------------------------------地球创建------------------------------------**/

        // Vue中引入方式：import { ESObjectsManager } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';

        const { ESObjectsManager } = XE2['esobjs-xe2-plugin-main']
        const objm = new ESObjectsManager();
        window.g_objm = objm;

        // 创建Cesium视口
        const viewer = objm.createCesiumViewer({
            domid: document.getElementById("app"),
        });
        viewer.ionAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyZmQzMDI1OC1kMWYyLTQ1ZjEtYTJmNS0yMjY1ZDcxZjEyOTkiLCJpZCI6NjQ3MTgsImlhdCI6MTYyOTQzNDM5M30.m8vkzG05QiAfe6JQ0XPK8z_6KuUVMf_CoSY-YlMnAIg"
        viewer.statusChanged.don(status => {
            if (status == 'Created') {
                objm.activeViewer.flyIn([
                    116.38836950514933,
                    39.90152264173749,
                    2558.2266625402162
                ], [
                    359.96364396289476,
                    -48.66554801391198,
                    0.0011063677408178429
                ], 3)

            }
        })
        // 通过json创建一个影像图层
        const imageryLayer = objm.createSceneObjectFromJson({
            "id": "9812a65f-0de0-4f7b-b234-809c0c2fb8ef",
            "type": "ESImageryLayer",
            "maximumLevel": 18,
            "url": "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
        });

        /**
         * 如何加载json格式的相机视角？json在哪里配置？
         * 1.首先通过EarthUi https://www.earthsdk.com/earthui/index.html 将场景中加载好的相机视角保存为json文件
         * 2.将json赋值给对象管理器的views
         * 3.然后通过cameraViewsManager视角管理器就可拿到设置的相机视角views
        */
        // 2
        const views = [
            {
                "duration": 1,
                "position": [
                    116.38836950514933,
                    39.90152264173749,
                    2558.2266625402162
                ],
                "rotation": [
                    359.96364396289476,
                    -48.66554801391198,
                    0.0011063677408178429
                ],
                "thumbnail": "",
                "name": "视角1"
            },
            {
                "duration": 1,
                "position": [
                    116.39355837748668,
                    39.97945849253805,
                    425.7974209221731
                ],
                "rotation": [
                    359.52382957543205,
                    -54.15362525868217,
                    0.00022456053970542513
                ],
                "thumbnail": "",
                "name": "视角2"
            },
            {
                "duration": 1,
                "position": [
                    116.60748747942176,
                    40.04720047083411,
                    2484.0350082986993
                ],
                "rotation": [
                    352.3304804630354,
                    -45.37457183515052,
                    0.0009119860607667718
                ],
                "thumbnail": "",
                "name": "视角3"
            }
        ]

        objm.cameraViewsManager.views = [...views]

        // 3
        const viewCollection = objm.cameraViewsManager
        /** -----------------------Vue组件方式创建UI----------------------------------**/
        const VueOption = {
            template: ` 
            <div class="views" :key="key">
                <div class="header">
                    <button @click="appendView">添加视角</button>
                    <button @click="clearAllViews">清空全部视角</button>
                </div>
                <div class="content">
                    <div v-for="(item, index) in viewCollection.views" :key="index">
                        <img :class="{ 'focus': currentIndex == index }" @click="flyToView(index)" :src="item.thumbnail" alt="">
                        <input type="text" :value="item.name" @blur="resetViewName(index, $event)">
                        <span class="close"  @click="deleteView(index)">❎</span>
                    </div>
                </div>
            </div>
            `,
            data() {
                return {
                    key: 0,
                    currentIndex: 0
                }
            },
            beforeMount() {
                // 该viewCollection是ESCameraViewCollection的实例
                this.viewCollection = viewCollection

                /** -----------------------事件的监听----------------------------------**/
                // 监听视角变化
                this.viewCollection.viewsChanged.don(() => {
                    this.key = this.key + 1
                })
                // 监听当前索引值
                this.viewCollection.currentViewIndexChanged.don((val) => {
                    this.currentIndex = val
                })
            },
            methods: {
                // 添加视角
                appendView() {
                    this.viewCollection.appendView()
                },
                // 飞向视角并设置当前索引值
                flyToView(index) {
                    this.viewCollection.flyToView(index)
                    this.viewCollection.setCurrentView(index)
                },
                // 重命名视角
                resetViewName(index, $event) {
                    this.viewCollection.resetViewName(index, $event.target.value)
                },
                // 清空全部视角
                clearAllViews() {
                    this.viewCollection.clearAllViews()
                },
                // 删除视角
                deleteView(index) {
                    this.viewCollection.deleteView(index)
                }
            },

        }
        const vm1 = Vue.createApp({
            components: {
                viewCollection: VueOption
            }
        })
        vm1.mount("#panel");


    </script>



    <!-- 切换UE和Cesium引擎的组件（Vue） -->
    <div id="root">
        <switch-engine :objm="objm"></switch-engine>
    </div>
    <!-- 切换UE和Cesium的组件 -->
    <script src="./js/switchEngineCom.js"></script>

</body>

</html>